package cn.edu.jxau.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.jxau.domain.user;
import cn.edu.jxau.service.checkMessage;
import cn.edu.jxau.service.setMessage;

public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		// 从表单中获取用户输入的信息
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String flag = request.getParameter("remember");
		// 检查用户名和密码并且向cookie中设置用户信息
		checkMessage checkMessage=new checkMessage();
		user user = checkMessage.checkInfo(username);
		
		HttpSession session = request.getSession();
		if (user != null) {
			if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
				// 向Session中设置验证信息
				session.setAttribute("flag", "ok");
				session.setAttribute("username", username);
				setMessage.setCookies(username, password, flag, response);
				checkMessage.updataState(username,"在线");//修改数据库里用户状态
				response.sendRedirect("index.jsp");
			} else {
				response.sendRedirect("error.html");
			}

		} else {

			response.sendRedirect("error.html");
		}

	}

}
